<?php
//-------------------------------------------------------
// ShuAnG
// 档案管理
//-------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
class ArchiveManage extends Controller
{

    /**
     * @author  ShuAnG
     * @createDate  2023-03-10
     * 固定资产管理--列表
     * @return array
     */
    public function fixedAssetManagement(){
//        department_id : 1.技术部 2.行政部 3.营销部
//        device_id : 1.电脑主机 2.显示器 3.饮水机 4.空调 5.冰箱 6.打印机 7.其它
//        fixed_asset 表

        if(input()['limit'])
        {
            $page = input()['page']?:1;
            $limit = input()['limit']?:1;
            $db = Db('fixed_asset');
            $count = $db;

            if(input('keyword'))
            {
                $keyWord = input('keyword');
                $list_arr = $db->where('device_type_num|device_num|belong_to_personal','LIKE',"%$keyWord%")->page($page,$limit)->select();
                $count = $count->where('device_type_num|device_num|belong_to_personal','LIKE',"%$keyWord%")->count();
            }else{
                $list_arr = $db->page($page,$limit)->select();
                $count = $count->count();
            }

            foreach($list_arr as $key => $val){
                if($val['department_id'] == 1){
                    $list_arr[$key]['department_name'] = '技术部';
                }elseif($val['department_id'] == 2){
                    $list_arr[$key]['department_name'] = '行政部';
                }elseif($val['department_id'] == 3){
                    $list_arr[$key]['department_name'] = '营销部';
                }

                switch($val['device_id']){
                    case 1:
                        $list_arr[$key]['device_name'] = '电脑主机';
                        break;
                    case 2:
                        $list_arr[$key]['device_name'] = '显示器';
                        break;
                    case 3:
                        $list_arr[$key]['device_name'] = '饮水机';
                        break;
                    case 4:
                        $list_arr[$key]['device_name'] = '空调';
                        break;
                    case 5:
                        $list_arr[$key]['device_name'] = '冰箱';
                        break;
                    case 6:
                        $list_arr[$key]['device_name'] = '打印机';
                        break;
                    case 7:
                        $list_arr[$key]['device_name'] = '其它';
                        break;
                }

                $list_arr[$key]['enable_time_date'] = date('Y-m-d',$val['enable_time']);
            }

            return ['code' => 0, 'count' => $count, 'data' => $list_arr, 'msg'=>''];
        }else{

            return view();
        }

    }

    /**
     * @author  ShuAnG
     * @createDate  2023-03-10
     * 固定资产管理--添加
     * @return array
     */
    public function fixedAssetAdd()
    {
        if(request()->isAjax())
        {
            $data = input('post.');

            if($data['enable_time']){
                $data['enable_time'] = strtotime($data['enable_time']);
            }
            $admin_info = Db('admin')->where('id',$data['belong_to_personal'])->find();
            if(empty($admin_info)){
                ['msg'=>'没有该负责人','icon'=>2];
            }
            $data['admin_id'] = $admin_info['id'];
            $data['belong_to_personal'] = $admin_info['username'];

            $data['remarks'] = html_entity_decode($data['remarks']);

            if(input('id')){
                $res = db('fixed_asset')->where('id',input('id'))->update($data);
            }else{
                $res = db('fixed_asset')->insert($data);
            }

            return $res?['msg'=>'操作成功','icon'=>1]:['msg'=>'操作失败','icon'=>2];
        }else{
            if(input('id')){
                $fixed_asset_info = Db('fixed_asset')->where('id',input('id'))->find();

                $this->assign([
                    'data' => $fixed_asset_info,
                ]);
            }

            $admin_list = Db('admin')->where('status',1)->field('id,username')->select();

            $this->assign([
                'admin_list' => $admin_list,
            ]);

            return view();
        }
    }

    /**
     * @author  ShuAnG
     * @createDate  2023-03-10
     * 固定资产管理--详情
     * @return mixed
     */
    public function fixedAssetView(){
        if(input('id')){
            $data = Db('fixed_asset')->where('id',input('id'))->find();

            if($data['department_id'] == 1){
                $data['department_name'] = '技术部';
            }elseif($data['department_id'] == 2){
                $data['department_name'] = '行政部';
            }elseif($data['department_id'] == 3){
                $data['department_name'] = '营销部';
            }

            switch($data['device_id']){
                case 1:
                    $data['device_name'] = '电脑主机';
                    break;
                case 2:
                    $data['device_name'] = '显示器';
                    break;
                case 3:
                    $data['device_name'] = '饮水机';
                    break;
                case 4:
                    $data['device_name'] = '空调';
                    break;
                case 5:
                    $data['device_name'] = '冰箱';
                    break;
                case 6:
                    $data['device_name'] = '打印机';
                    break;
                case 7:
                    $data['device_name'] = '其它';
                    break;
            }

            $this->assign([
                'data' => $data
            ]);

            return view();
        }
    }

    /**
     * @author  ShuAnG
     * @createDate  2023-03-10
     * 固定资产管理--删除
     * @return array
     */
    public function fixedAssetDel(){
        if(input('id')){
            if(request()->isAjax()){
                $re = Db('fixed_asset')->where('id',input('id'))->delete();

                return $re?['msg'=>'操作成功','icon'=>1]:['msg'=>'操作失败','icon'=>2];
            }
        }
    }

    /**
     * @author  ShuAnG
     * @createDate  2023-03-11
     * 人事档案管理--列表
     * @return array
     */
    public function personnelArchive(){
//        personnel_archive
//        部门 ：1.鲸 2.权 3.赞 4.创
//        学历: 1.中专 2.大专 3.本科
//        紧急联系人关系：1.父亲 2.母亲 3.夫妻 4.兄妹 5.子女 6.朋友 7.老表

        if(input()['limit'])
        {
            $page = input()['page']?:1;
            $limit = input()['limit']?:1;
            $db = Db('personnel_archive');
            $count = $db;

            if(input('keyword'))
            {
                $keyWord = input('keyword');
                //姓名、身份证、家庭住址、手机号、紧急联系人姓名、紧急联系人手机号
                $list_arr = $db->where('name|ID_num|home_address|phone_num|emergency_name|emergency_phone','LIKE',"%$keyWord%")->page($page,$limit)->select();
                $count = $count->where('name|ID_num|home_address|phone_num|emergency_name|emergency_phone','LIKE',"%$keyWord%")->count();
            }else{
                $list_arr = $db->page($page,$limit)->select();
                $count = $count->count();
            }

            //关联 auth_group 表
            $auth_group_list = Db('auth_group')->where('id','neq',1)->select();

            foreach($list_arr as $key => $val){
                $val['sex'] == 1 ? $list_arr[$key]['sex_name'] = '男' : $list_arr[$key]['sex_name'] = '女';
                $val['marriage'] == 1 ? $list_arr[$key]['marriage_name'] = '已婚' : $list_arr[$key]['marriage_name'] = '未婚';
                $val['formal'] == 1 ? $list_arr[$key]['formal_name'] = '已转正' : $list_arr[$key]['formal_name'] = '未转正';
                $val['social_security_benefits'] == 1 ? $list_arr[$key]['social_security_benefits_name'] = '已缴纳' : $list_arr[$key]['social_security_benefits_name'] = '未缴纳';
                $list_arr[$key]['join_time_date'] = date('Y-m-d',$val['join_time']);

                foreach($auth_group_list as $k => $v){
                    if($val['auth_group_id'] == $v['id']){
                        $list_arr[$key]['auth_group_title'] = $v['title'];
                    }
                }

                switch($val['education_level']){
                    case 1:
                        $list_arr[$key]['education_level_name'] = '中专';
                        break;
                    case 2:
                        $list_arr[$key]['education_level_name'] = '大专';
                        break;
                    case 3:
                        $list_arr[$key]['education_level_name'] = '本科';
                        break;
                }
            }

            return ['code' => 0, 'count' => $count, 'data' => $list_arr, 'msg'=>''];
        }else{

            return view();
        }
    }
    
    /**
     * @author  ShuAnG
     * @createDate  2023-03-11
     * 人事档案管理--添加
     * @return array
     */
    public function personnelArchiveAdd(){
        if(request()->isAjax())
        {
            $data = input('post.');

            if($data['join_time']){
                $data['join_time'] = strtotime($data['join_time']);
            }

            $admin_info = Db('admin')->where('id',$data['belong_to_personal'])->find();

            if(empty($admin_info)){
                ['msg'=>'没有该负责人','icon'=>2];
            }

            $data['admin_id'] = $data['belong_to_personal'];
            $data['name'] = $admin_info['name'];

            if(input('id')){
                $res = db('personnel_archive')->where('id',input('id'))->update($data);
            }else{
                $res = db('personnel_archive')->insert($data);
            }

            return $res?['msg'=>'操作成功','icon'=>1]:['msg'=>'操作失败','icon'=>2];
        }else{
            if(input('id')){
                $personnel_archive_info = Db('personnel_archive')->where('id',input('id'))->find();

                $this->assign([
                    'data' => $personnel_archive_info,
                ]);
            }

            $admin_list = Db('admin')->where('status',1)->field('id,username,name')->select();

            $auth_group_list = Db('auth_group')->where('id','neq',1)->field('id,title')->select();

            $this->assign([
                'admin_list' => $admin_list,
                'auth_group_list' => $auth_group_list,
            ]);

            return view();
        }

    }

    /**
     * @author  ShuAnG
     * @createDate  2023-03-11
     * 人事档案管理--详情
     * @return mixed
     */
    public function personnelArchiveView(){
        if(input('id')){
            $data = Db('personnel_archive')->where('id',input('id'))->find();
            //关联 auth_group 表
            $auth_group_list = Db('auth_group')->where('id','neq',1)->select();

            switch($data['department_id']){
                case 1:
                    $data['department_name'] = '鲸';
                    break;
                case 2:
                    $data['department_name'] = '权';
                    break;
                case 3:
                    $data['department_name'] = '赞';
                    break;
                case 4:
                    $data['department_name'] = '创';
                    break;
            }

            switch($data['education_level']){
                case 1:
                    $data['education_name'] = '中专';
                    break;
                case 2:
                    $data['education_name'] = '大专';
                    break;
                case 3:
                    $data['education_name'] = '本科';
                    break;
            }

            switch($data['emergency_relation']){
                case 1:
                    $data['emergency_relation_name'] = '父亲';
                    break;
                case 2:
                    $data['emergency_relation_name'] = '母亲';
                    break;
                case 3:
                    $data['emergency_relation_name'] = '夫妻';
                    break;
                case 4:
                    $data['emergency_relation_name'] = '兄妹';
                    break;
                case 5:
                    $data['emergency_relation_name'] = '子女';
                    break;
                case 6:
                    $data['emergency_relation_name'] = '朋友';
                    break;
                case 7:
                    $data['emergency_relation_name'] = '老表';
                    break;
            }

            foreach($auth_group_list as $key => $val){
                if($val['id'] == $data['auth_group_id']){
                    $data['auth_group_title'] = $val['title'];
                }
            }

            $data['sex'] == 1 ? $data['sex_name'] = '男' : $data['sex_name'] = '女';
            $data['marriage'] == 1 ? $data['marriage_name'] = '已婚' : $data['marriage_name'] = '未婚';
            $data['formal'] == 1 ? $data['formal_name'] = '已转正' : $data['formal_name'] = '未转正';
            $data['social_security_benefits'] == 1 ? $data['social_security_benefits_name'] = '已缴纳' : $data['social_security_benefits_name'] = '未缴纳';

            $this->assign([
                'data' => $data
            ]);

            return view();
        }
    }

    /**
     * @author  ShuAnG
     * @createDate  2023-03-11
     * 人事档案管理--删除
     * @return array
     */
    public function personnelArchiveDel(){
        if(input('id')){
            if(request()->isAjax()){
                $re = Db('personnel_archive')->where('id',input('id'))->delete();

                return $re?['msg'=>'操作成功','icon'=>1]:['msg'=>'操作失败','icon'=>2];
            }
        }
    }



}